package com.example.demo.cashier;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;


@SuppressWarnings("serial")
public class Mall implements ActionListener {

    private static JLabel unitPriceLabel;
    private static JTextField unitPriceValue;
    private static JLabel numLabel;
    private static JTextField numValue;

    private double totalPrice = 0;
    private static JList<String> jList;
    private static DefaultListModel<String> listModel;
    private static JLabel totalNum;
    private static JLabel calcType;
    private static JComboBox<String> jComboBox;

    public static void main(String[] args) {
        JFrame frame = new JFrame("商城收银系统");
        frame.setSize(450, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        frame.add(panel);
        placeComponents(panel);
        frame.setVisible(true);
    }

    private static void placeComponents(JPanel panel) {
        panel.setLayout(null);
        Mall mainFrame = new Mall();
        // 第一行
        unitPriceLabel = new JLabel("单价:");
        unitPriceLabel.setBounds(10, 20, 80, 25);
        panel.add(unitPriceLabel);

        unitPriceValue = new JTextField(20);
        unitPriceValue.setBounds(100, 20, 165, 25);
        panel.add(unitPriceValue);

        JButton confirmButton = new JButton("确定");
        confirmButton.setBounds(280, 20, 80, 25);
        confirmButton.addActionListener(mainFrame);
        confirmButton.setActionCommand("confirm");
        panel.add(confirmButton);

        // 第二行
        numLabel = new JLabel("数量:");
        numLabel.setBounds(10, 50, 80, 25);
        panel.add(numLabel);

        numValue = new JTextField(20);
        numValue.setBounds(100, 50, 165, 25);
        panel.add(numValue);

        JButton resetButton = new JButton("重置");
        resetButton.setBounds(280, 50, 80, 25);
        resetButton.addActionListener(mainFrame);
        resetButton.setActionCommand("reset");
        panel.add(resetButton);

        // 第三行
        calcType = new JLabel("计算方式:");
        calcType.setBounds(10, 90, 80, 25);
        panel.add(calcType);
        jComboBox = new JComboBox<String>();
        jComboBox.insertItemAt("正常收费", 0);
        jComboBox.insertItemAt("打8折", 1);
        jComboBox.insertItemAt("满100返10", 2);
        jComboBox.setSelectedIndex(0);
        jComboBox.setBounds(100, 90, 80, 25);
        panel.add(jComboBox);

        // 第四行
        listModel = new DefaultListModel<String>();
        jList = new JList<String>(listModel);
        jList.setBounds(10, 120, 400, 180);
        panel.add(jList);

        // 第五行
        JLabel totalLabel = new JLabel("总计：");
        totalLabel.setBounds(10, 300, 80, 25);
        panel.add(totalLabel);
        totalNum = new JLabel("0.00");
        totalNum.setBounds(100, 300, 80, 25);
        panel.add(totalNum);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        if (event.getActionCommand().equals("confirm")) {
            double unitPrice = Double.parseDouble(unitPriceValue.getText());
            double num = Double.parseDouble(numValue.getText());
            CashContext cashContext = new CashContext(jComboBox.getSelectedItem().toString());
            double total = cashContext.getResult(unitPrice*num);
            totalPrice += total;
            listModel.addElement("单价：" + unitPriceValue.getText() + " 数量：" + numValue.getText() + " "
                    + jComboBox.getSelectedItem() + " 合计：" + total);
            totalNum.setText(totalPrice + "");
        } else if (event.getActionCommand().equals("reset")) {
            unitPriceValue.setText("");
            numValue.setText("");
            listModel.clear();
            totalPrice = 0;
            totalNum.setText(totalPrice + "");
        }
    }

}
